package com.yphone.ui.pay.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zzkj.yunphone.R;

public class SelectNumLayout extends FrameLayout {

    private TextView tv_num;
    private View tv_add;
    private View tv_minus;
    private OnNumChangeListener listener;

    public SelectNumLayout(@NonNull Context context) {
        this(context, null);
    }

    public SelectNumLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectNumLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();

    }

    private void initView() {
        View.inflate(getContext(), R.layout.view_pay_select_nul,this);
        tv_num = findViewById(R.id.tv_num);
        tv_add = findViewById(R.id.tv_add);
        tv_minus = findViewById(R.id.tv_minus);
        tv_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setNum(true);
            }
        });
        tv_minus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setNum(false);
            }
        });
    }

    public int getNum(){
        return Integer.parseInt(tv_num.getText().toString());
    }

    private void setNum(boolean isAdd){
        int num = getNum();
        if (!isAdd){
            if (num == 1){
            }else {
                num--;
                tv_num.setText(String.valueOf(num));
            }
        }else {
            num++;
            tv_num.setText(String.valueOf(num));
        }
        listener.onChange(Integer.parseInt(tv_num.getText().toString()));
    }

    public void setNumChangeListener(OnNumChangeListener listener){
        this.listener = listener;
    }

    public interface OnNumChangeListener{
        void onChange(int num);
    }

}
